package com.mfc.education.service.edu.controller.api;


import com.mfc.education.common.base.result.R;
import com.mfc.education.common.base.util.JwtInfo;
import com.mfc.education.common.base.util.JwtUtils;
import com.mfc.education.service.edu.entity.vo.CourseCollectVo;
import com.mfc.education.service.edu.service.CourseCollectService;
import com.mfc.education.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */

@Api(description = "课程收藏")
@RestController
@RequestMapping("api/edu/course-collect")
public class ApiCourseCollectController {

    @Autowired
    private CourseCollectService courseCollectService;


    @ApiOperation("根据课程id和memberId保存收藏课程")
    @PostMapping("auth/save/{courseId}")
    public R saveCourseCollect(@ApiParam(value = "课程id", required = true)
                               @PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);

        boolean result = courseCollectService.saveCollect(courseId, jwtInfo.getId());
        if (result) {
            return R.ok().message("收藏成功");
        } else {
            return R.error().message("收藏失败");
        }
    }


    @ApiOperation("根据课程id和memberId取消收藏课程")
    @DeleteMapping("auth/remove/{courseId}")
    public R removeCourseCollect(@ApiParam(value = "课程id", required = true)
                               @PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);

        boolean result = courseCollectService.removeCourseCollect(courseId, jwtInfo.getId());
        if (result) {
            return R.ok().message("取消收藏");
        } else {
            return R.error().message("取消收藏失败");
        }
    }


    @ApiOperation("根据memberId查找收藏课程列表")
    @GetMapping("auth/listMy")
    public R courseCollectList(HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);

        List<CourseCollectVo> collectVoList = courseCollectService.courseCollectListMy(jwtInfo.getId());

        return R.ok().data("items",collectVoList);
    }


    @ApiOperation("根据课程id和memberId查询是否收藏了该课程")
    @GetMapping("auth/is-collect/{courseId}")
    public R selectCourseCollect(@ApiParam(value = "课程id", required = true)
                               @PathVariable String courseId, HttpServletRequest request) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);

        boolean result = courseCollectService.selectCollect(courseId, jwtInfo.getId());

        return R.ok().data("isCollect",result);

    }
}

